<?php

class AdministrationView {

	private $removeButton = "Remove";
	private $userToRemove = "UserToRemove";
	
	private $changePassButton = "Change";
	private $userToChange = "UserToChange";

	private $removeMessage;
	private $changePassMessage;
	
	/**
	 * Function to find out if admin trid to remove user
	 * @access public
	 * @param -
	 * @return Bool
	 */
	public function didAdminTryToRemoveUser() {
		if (isset($_POST[$this -> removeButton])) {
			return true;
		}
		return false;
	}
	
	/**
	 * Function to find out if admin trid to Update password on user
	 * @access public
	 * @param -
	 * @return Bool
	 */
	public function didAdminChangePasswordOnUser(){
		if(isset($_POST[$this->changePassButton])){
			return true;
		}
		return false;
	}
	
	/**
	 * Function to get ID of user to remove
	 * @access public 
	 * @param -
	 * @return int ID
	 */
	public function getUserIdToRemove() {
		if (isset($_POST[$this -> userToRemove])) {
			return $_POST[$this -> userToRemove];
		}
		return false;
	}
	
	/**
	 * Function to get ID of user to Update password on
	 * @access public 
	 * @param -
	 * @return int ID
	 */
	public function getUserIdToChangePassword(){
		if(isset($_POST[$this->userToChange])){
			return $_POST[$this->userToChange];
		}
		return false;
	}
	
	/**
	 * Function to generate confirmation for UserRemove
	 * @access public 
	 * @param UserObject
	 * @return HTML
	 */
	public function generateRemoveUserConfirm($user) {
		if (is_object($user)) {
			$this -> removeMessage = "<p class='noerrorP'>User was <strong>Removed</strong></p>";
			return;
		}
		$this -> removeMessage = "<p class='fileErrorP'>User was <strong>NOT Removed</strong></p>";
		Return;
	}
	
	/**
	 * Function to generate confirmation for UserPasswordUpdate
	 * @access public 
	 * @param UserObject
	 * @return HTML
	 */
	public function generateChangePassConfirm($user){
		if(is_object($user)){
			$this->changePassMessage = "<p class='noerrorP'>User $user->UserName has been updated, new password is:<strong> $user->PassWord </strong></p>";
		}
		else{
			$this->changePassMessage = "<p class='fileErrorP'>Password was <strong>NOT changed</strong></p>";
		}	
	}
	
	/**
	 * Function to generate adminview div
	 * @access public
	 * @param Array with all users
	 * @return HTML
	 */
	public function generateAdminView($a_allUsersArray) {
		$returnDiv = "<div class='adminDiv'>
						<h2>Admin view | Go go godzilla</h2>
							<p>The Admin view of <strong>YAIC</strong></p>
								$this->removeMessage
								$this->changePassMessage
						</div><div class='adminDiv'>
						<div class='removgenDiv'>
							<form action='' method='post' class='adminForm'>
								<fieldset>
									<legend><strong>Remove a user</strong></legend> </br>";

		foreach ($a_allUsersArray as $key => $user) {
			if ($user -> IsAdmin) {
				continue;
			}
			$returnDiv .= "<input type='radio' name='" . $this -> userToRemove . "' value='$user->userId' >$user->UserName</br>";
		}

		$returnDiv .= "<input type='submit' value='Remove' name='" . $this -> removeButton . "'
							</fieldset>
						</form>
						</div>";
						
		//Call private function to generate a list of users for passwordupdate			
		$returnDiv .= $this->generateChangePaswordForm($a_allUsersArray);
		
		$returnDiv .= "<div class='clear'></div></div>";
		
		//Add Logged messages
		$returnDiv .= "<div class='adminDiv'>
				".LogView::showAllMessages()."
		</div>";
					
		return $returnDiv;
	}
	
	/**
	 * Function to generate form for updating password, called by self->generateAdminView()
	 * @access private
	 * @param Array with all users
	 * @return HTML
	 */
	private function generateChangePaswordForm($a_allUsersArray){
		$returnDiv = "<div class='removgenDiv'>
		<form action='' method='post' class='adminForm'>
							<fieldset>
								<legend><strong>Generate Password</strong></legend> </br>";

		foreach ($a_allUsersArray as $key => $user) {
			if ($user -> IsAdmin) {
				continue;
			}
			$returnDiv .= "<input type='radio' name='" . $this ->userToChange . "' value='$user->userId' >$user->UserName</br>";
		}

		$returnDiv .= "<input type='submit' value='Change' name='" . $this -> changePassButton . "'
							</fieldset>
						</form></div>";
		
		return $returnDiv;
	}

}
